import { Component, OnInit } from '@angular/core';
import {Path} from "../../../../portal/admin/admin-breadcrumb/breadcrumb";
import {PageSubject} from "../../../../util/page-subject";
import {MsgResult} from "../../../../models/msg-result";
import {MsgConstants} from "../../../../constants/constants";
import {OneDialogService} from "../../../../widgets/one-dialog/one-dialog.service";
import {ResponseUtil} from "../../../../util/response-util";
import {CountyRankingService} from "./county-ranking.service";
import {CountyRanking} from "./county-ranking";
import {TableUtil} from "../../../../util/table-util";

declare var $;
declare var moment;

@Component({
  selector: 'app-county-ranking',
  templateUrl: './county-ranking.component.html',
  styleUrls: ['./county-ranking.component.css']
})
export class CountyRankingComponent implements OnInit {
  config:any;
  table:any;
  ranking:CountyRanking;
  constructor(
    private pageSubject : PageSubject,
    private CountyRankingService : CountyRankingService,
    private responseUtil :ResponseUtil,
    private tableUtil:TableUtil,
    private dialogService: OneDialogService,
  ) {
    this.initObject();
  }

  ngOnInit() {
    this.pushPage();
    this.initTime();
    this.tableContent();
    this.tableContentData(this.ranking);
  };

  /**
   * 面包屑导航
   */
  pushPage() {
    let path = new Path("城市排名", "");
    this.pageSubject.pushPage("区县排名", "", path);
  }

  /**
   * 实例化对象
   */
  private initObject(){
    this.ranking = new CountyRanking(moment().subtract(1,"h").format("YYYY-MM-DD HH")+":00:00",moment().subtract(1,"h").format("YYYY-MM-DD HH")+":00:00","allScope","hourData");
  }
  //初始化时间插件
  initTime(){
    let timeData = moment().subtract(1,"h").format("YYYY-MM-DD HH")+":00"
    this.config = {
      singleDatePicker: true,
      timePicker:true,
      timePickerIncrement:5,
      timePicker24Hour:true,
      parentEl:$(".wrapper"),
      maxDate:timeData,
      locale:{
        format:"YYYY-MM-DD HH:00",
        applyLabel : '确定',
        cancelLabel : '取消',
        fromLabel: '从',
        toLabel: '到',
        weekLabel: 'W',
        customRangeLabel: 'Custom Range',
        daysOfWeek:["日","一","二","三","四","五","六"],
        monthNames: ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],
        firstDay:1
      },
      startDate:timeData,
      endDate:timeData
    };
    $("#countyRankingDate").daterangepicker(this.config);
  }

  //更改区域
  clickScopeType2(type){
    this.ranking.scopeType = type;
  }

  //更改时间
  clickDataType2(type){
    this.ranking.dataType = type;
    if(type == "hourData"){
      this.config.locale.format="YYYY-MM-DD HH:00";
      this.config.timePicker=true;
      this.config.startDate=moment().subtract(1,"h").format("YYYY-MM-DD HH")+":00:00";
      this.config.maxDate=moment().subtract(1,"h").format("YYYY-MM-DD HH")+":00:00";
    }else{
      this.config.locale.format="YYYY-MM-DD";
      this.config.timePicker=false;
      this.config.startDate=moment().subtract(1,"d").format("YYYY-MM-DD");
      this.config.maxDate=moment().subtract(1,"d").format("YYYY-MM-DD");
    }
    $('#countyRankingDate').daterangepicker(this.config);
  }

  //点击查询
  search(){
    $(".one-data-loadState").show();
    let startTime = $("#countyRankingDate").val();
    startTime+=startTime.split(" ").length==1?" 00:00:00":":00";
    this.ranking.startTime = this.ranking.endTime = startTime;
    this.tableContentData(this.ranking);
  }

  //表格数据
  tableContentData(obj){
    this.CountyRankingService.getCountyRankingData(obj).subscribe(
      result => this.responseUtil.success(result,this.tableContentDataResult),
      message => this.responseUtil.error(message)
    );
  }
  private tableContentDataResult = (result: MsgResult) =>{
    if (result && result.code == MsgConstants.MSG_CODE_OK) {
      this.tableContentDataTree(result.data);
    }
    if (result && result.code != MsgConstants.MSG_CODE_OK) {
      this.dialogService.info(result.message);
    }
  }
  private tableContentDataTree(data){
    $(".one-data-loadState").hide();
      this.table.bootstrapTable("load",data);

  }
  //表格
  tableContent(){
    this.table = this.tableUtil.load("countyRankingTable",{
      columns: [
        [{
          field: 'index',
          title: '排名',
          rowspan: 2,
          valign:"middle",
          width:35
        }, {
          field: 'regionName',
          title: '城市',
          align:"left",
          halign:"center",
          rowspan: 2,
          valign:"middle",
          width:90
        }, {
          field: 'compositeValue',
          title: '综合指数',
          rowspan: 2,
          valign:"middle",
          width:60,
          align:"right",
          halign:"center"
        }, {
          field: 'aqi',
          title: 'AQI',
          rowspan: 2,
          valign:"middle",
          width:50,
          align:"right",
          halign:"center"
        }, {
          field: ' ',
          title: 'PM10',
          colspan: 2,
          width:100
        }, {
          field: ' ',
          title: 'PM2.5',
          colspan: 2,
          width:100
        }, {
          field: ' ',
          title: 'SO<sub>2</sub>',
          colspan: 2,
          width:100
        }, {
          field: ' ',
          title: 'NO<sub>2</sub>',
          colspan: 2,
          width:100
        }, {
          field: ' ',
          title: 'CO',
          colspan: 2,
          width:100
        }, {
          field: ' ',
          title: 'O<sub>3</sub>',
          colspan: 2,
          width:100
        }],[
          {
            field: 'pm10',
            title: '浓度',
            align:"right",
            width:50,
            halign:"center"
          },{
            field: 'pm10IAQI',
            title: '指数',
            align:"right",
            width:50,
            halign:"center"
          },{
            field: 'pm25',
            title: '浓度',
            align:"right",
            width:50,
            halign:"center"
          },{
            field: 'pm25IAQI',
            title: '指数',
            align:"right",
            width:50,
            halign:"center"
          },{
            field: 'so2',
            title: '浓度',
            align:"right",
            width:50,
            halign:"center"
          },{
            field: 'so2IAQI',
            title: '指数',
            align:"right",
            width:50,
            halign:"center"
          },{
            field: 'no2',
            title: '浓度',
            align:"right",
            width:50,
            halign:"center"
          },{
            field: 'no2IAQI',
            title: '指数',
            align:"right",
            width:50,
            halign:"center"
          },{
            field: 'co',
            title: '浓度',
            align:"right",
            width:50,
            halign:"center"
          },{
            field: 'coIAQI',
            title: '指数',
            align:"right",
            width:50,
            halign:"center"
          },{
            field: 'o3',
            title: '浓度',
            align:"right",
            width:50,
            halign:"center"
          },{
            field: 'o3IAQI',
            title: '指数',
            align:"right",
            width:50,
            halign:"center"
          }
        ]
      ],
      data:[],
      striped :true,
      undefinedText : " ",
      height:$(window.innerHeight)[0]-224
    });
  }

  //添加导出按钮
  exportBtn(){
    var objString = "endTime="+this.ranking.endTime+"&startTime="+this.ranking.startTime+"&dataType="+this.ranking.dataType+"&scopeType="+this.ranking.scopeType;
    let baseUrl = location.protocol + "//" + location.host + "/";
    window.location.href = baseUrl+"api/exportExcel/areaComprehensiveIndexDataExcelExport?format=json&sortScopeType=areaCountySort&"+objString;
  };

}
